//
//  BQShopTableViewController.m
//  BeeQuick
//
//  Created by Asun on 2017/7/9.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQShopTableViewController.h"
#import "BQShopTipView.h"
#import "BQShopHeaderView.h"
#import "BQShopCell.h"
#import "BQMineReceiveController.h"
#import "BQHomeTableViewController.h"
#import "BQProductModel.h"
#import "BQShopModelArrayManager.h"
#import "BQShopBottomView.h"
#import "BQShopPayController.h"
#define KWidth  self.view.frame.size.width
#define KHeight (self.view.frame.size.height - 64)
#define KMagin 15

@interface BQShopTableViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,weak)BQShopBottomView *bottomView;

@end

@implementation BQShopTableViewController{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [BQShopModelArrayManager manager].shopBlock = ^(){
        [self.tableView reloadData];
        self.bottomView.prcieLabelText = [self showPrice];
        if ([BQShopModelArrayManager manager].modelArray.count <= 3) {
            [self addTipView];
        }
    };
    if ([BQShopModelArrayManager manager].modelArray.count > 3) {
        [self addTableView];
    }else{
        [self addTipView];
            }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [BQShopModelArrayManager manager].modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    BQProductModel *model = [BQShopModelArrayManager manager].modelArray[indexPath.row];
    NSString *identfier;
    if (indexPath.row == 0) {
        identfier = @"BQShopLightningCell";
    }else if (indexPath.row == 1){
        identfier = @"BQShopTime";
    }else if (indexPath.row == 2){
        identfier = @"BQShopGet";
    }else{
        identfier = @"BQShopCell";
    }
    BQShopCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier forIndexPath:indexPath];
    model.tag = indexPath.row;
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 60;
    }
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}


- (void)leftItemAction{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (NSString *)showPrice{
    
    CGFloat  money = 0;
    for (int i = 3; i < [BQShopModelArrayManager manager].modelArray.count; i++) {
        BQProductModel  *model = [[BQProductModel alloc]init];
        model = [BQShopModelArrayManager manager].modelArray[i];
        money += model.price * model.count;
    }
    return [NSString stringWithFormat:@"共 $ %.2f",money];
}
- (void)viewWillAppear:(BOOL)animated{
    self.title = @"购物车";
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"v2_goback"] style: UIBarButtonItemStyleDone  target:self action:@selector(leftItemAction)];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)addTableView{
    UITableView *tableView = [[UITableView alloc]init];
    self.tableView = tableView;
    tableView.frame = self.view.frame;
    tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [tableView registerNib:[UINib nibWithNibName:@"BQShopCell" bundle:nil] forCellReuseIdentifier:@"BQShopCell"];
    [tableView registerNib:[UINib nibWithNibName:@"BQShopLightningCell" bundle:nil] forCellReuseIdentifier:@"BQShopLightningCell"];
    [tableView registerNib:[UINib nibWithNibName:@"BQShopTime" bundle:nil] forCellReuseIdentifier:@"BQShopTime"];
    [tableView registerNib:[UINib nibWithNibName:@"BQShopGet" bundle:nil] forCellReuseIdentifier:@"BQShopGet"];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.trailing.leading.bottom.equalTo(self.view);
    }];
    tableView.tableFooterView  = [UIView new];
    BQShopHeaderView *headerView = [[BQShopHeaderView alloc]initWithFrame:CGRectMake(0,0, KWidth, 130)];
    headerView.headerBlock = ^{
        BQMineReceiveController *receiveVC = [[BQMineReceiveController alloc]init];
        [self.navigationController pushViewController:receiveVC animated:NO];
    };
    tableView.tableHeaderView = headerView;
    [self addBottomView];
    
    
}

- (void)addBottomView{
    BQShopBottomView *bottomView = [[BQShopBottomView alloc]init];
    bottomView.bottomViewBlock = ^(){
        BQShopPayController *payVC = [[BQShopPayController alloc]init];
        [self.navigationController pushViewController:payVC animated:NO];
    };
    self.bottomView = bottomView;
    bottomView.prcieLabelText = [self showPrice];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.leading.equalTo(self.view);
        make.height.offset(60);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
}

- (void)addTipView{
    
    BQShopTipView *shopTipView = [[BQShopTipView alloc]init];
    shopTipView.tipViewBlock = ^(){
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    };
    [self.view addSubview:shopTipView];
    [shopTipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.leading.trailing.bottom.equalTo(self.view);
    }];
    
}

@end
